package com.zz.admin.userManagement.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zz.admin.userManagement.dto.UserRoleDto;
import com.zz.admin.userManagement.entity.UserRole;
import com.zz.admin.userManagement.mapper.UserRoleMapper;
import com.zz.admin.userManagement.service.UserRoleService;
import com.zz.common.base.enums.BaseExceptionEnum;
import com.zz.common.constants.BaseConstant;
import com.zz.common.exception.ExceptionFactory;
import com.zz.common.utils.common.UUIDGenerator;
import com.zz.common.utils.mapper.BeanMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户角色关联表 服务实现类
 *
 * @author zhang.lei
 * @since 2025-04-12 21:20:12
 */
@Service
@Slf4j
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public UserRoleDto queryById(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        UserRole userRole = userRoleMapper.selectById(id);
        if (userRole == null) {
            return null;
        }
        return BeanMapper.map(userRole, UserRoleDto.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserRoleDto save(UserRoleDto userRoleDto) {
        // 根据业务修改
        return insert(userRoleDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        // 根据业务修改
        UserRoleDto userRoleDto = new UserRoleDto();
        userRoleDto.setPkId(id);
        deleteById(userRoleDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserRoleDto insert(UserRoleDto userRoleDto) {
        userRoleDto.setPkId(UUIDGenerator.generateUUID());
        UserRole entity = BeanMapper.map(userRoleDto, UserRole.class);
        userRoleMapper.insert(entity);
        return userRoleDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserRoleDto insertWithId(UserRoleDto userRoleDto) {
        if (StringUtils.isBlank(userRoleDto.getPkId())) {
            userRoleDto.setPkId(UUIDGenerator.generateUUID());
        }
        UserRole entity = BeanMapper.map(userRoleDto, UserRole.class);
        userRoleMapper.insert(entity);
        return userRoleDto;
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateById(UserRoleDto userRoleDto) {
        if (StringUtils.isBlank(userRoleDto.getPkId())) {
            throw ExceptionFactory.createBaseException(BaseExceptionEnum.INTERNAL_SERVER_ERROR, "更新时，主键不能为空");
        }
        UserRole entity = BeanMapper.map(userRoleDto, UserRole.class);
        return userRoleMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteById(UserRoleDto userRoleDto) {
        return updateById(userRoleDto);
    }

}
